package lk.icta.core.dao.impl;

import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.ReportsRailwayDAO;
import lk.icta.core.dto.LeaseArrearsDetails;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nuwan
 * Date: 7/10/14
 * Time: 10:42 AM
 * To change this template use File | Settings | File Templates.
 */
public class ReportsRailwayDAOImpl extends BaseDAO implements ReportsRailwayDAO {

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<LeaseArrearsDetails> getLeaseArrearsReport(int districtId, int branchId, int railwayTrackId, double arrearsMax, double arrearsMin, User user) {
        List<LeaseArrearsDetails> leaseArrearsDetailses = new ArrayList<LeaseArrearsDetails>();

        String statisticsSQL = "select * from ELAND_RAIL.LeaseArrearsReport where district_id = " + "?" + " and branch_id = " + "?" + " and railwayTrack_id=" + "?";
        try {
            PreparedStatement preparedStatement = getConnection().prepareStatement(statisticsSQL);
            preparedStatement.setInt(1, districtId);
            preparedStatement.setInt(2, branchId);
            preparedStatement.setInt(3, railwayTrackId);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                LeaseArrearsDetails obj = new LeaseArrearsDetails();
                obj.setPaymentRentalAmount(rs.getDouble("paymentRentalAmount"));
                obj.setPaymentTaxAmount(rs.getDouble("paymentTaxAmount"));
                if ((0 == arrearsMin && 0 == arrearsMax) ||
                        (obj.getArrearsAmount() > arrearsMin && obj.getArrearsAmount() < arrearsMax)) {
                    obj.setAlienation_id(rs.getLong("alienation_id"));
                    obj.setLand_id(rs.getLong("land_id"));
                    obj.setReferenceNo(rs.getString("referenceNo"));
                    obj.setLastValuationPeriodStart(rs.getDate("lastValuationPeriodStart"));
                    obj.setLastValuationPeriodEnd(rs.getDate("lastValuationPeriodEnd"));
                    obj.setValuationAmount(rs.getDouble("valuationAmount"));
                    obj.setTotalPayable(rs.getDouble("totalPayable"));
                    leaseArrearsDetailses.add(obj);
                } else {
                    continue;
                }
            }
        } catch (SQLException e) {
            logger.error("Exception string @ getStatisticsForUserRole(): " + e.getMessage());
        } finally {
            closeConnection();
        }

        return leaseArrearsDetailses;
    }


}
